package com.luckku.boot.controller;


import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.luckku.boot.common.api.ApiResult;
import com.luckku.boot.model.dto.LoginDTO;
import com.luckku.boot.model.dto.RegisterDTO;
import com.luckku.boot.model.entity.User;
import com.luckku.boot.service.IUserService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

import static com.luckku.boot.jwt.JwtUtil.ACCOUNT;

/**
 * @author luckku
 * @date 2021/8/17
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;


    /**
     * /user/register
     * 用户注册
     *
     * @param dto RegisterDTO
     * @return ApiResult<Map < String, Object>>
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ApiResult<Map<String, Object>> register(@Valid @RequestBody RegisterDTO dto) {
        User user = userService.executeRegister(dto);
        if (ObjectUtils.isEmpty(user)) {
            return ApiResult.failed("账号注册失败");
        }
        Map<String, Object> map = new HashMap<>(16);
        map.put("user", user);
        return ApiResult.success(map);
    }

    /**
     * /user/login
     * 用户登录
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ApiResult<Map<String, String>> login(@Valid @RequestBody LoginDTO dto) {
        String token = userService.executeLogin(dto);
        if (ObjectUtils.isEmpty(token)) {
            return ApiResult.failed("账号密码错误");
        }
        Map<String, String> map = new HashMap<>(16);
        map.put("token", token);
        return ApiResult.success(map, "登录成功");
    }

    /**
     * /user/info
     * 获取用户信息
     *
     * @param account
     * @return ApiResult<User>
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public ApiResult<User> getUser(@RequestHeader(value = ACCOUNT) String account) {
        User user = userService.getUserByAccount(account);
        user.setUserId(user.getUserId());
        return ApiResult.success(user);
    }

    /**
     * /user/logout
     * 登出
     *
     * @return ApiResult<Object>
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ApiResult<Object> logOut() {
        return ApiResult.success(null, "注销成功");
    }


    /**
     * /user/{userId}
     *
     * @param userId 用户id
     * @param pageNo 当前页数
     * @param size   每页显示记录条数
     * @return
     */
    @GetMapping("/{userId}")
    public ApiResult<Map<String, Object>> getUserById(@PathVariable("userId") String userId,
                                                      @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> map = new HashMap<>(16);
        User user = userService.getById(userId);
        Assert.notNull(user, "用户不存在");
        map.put("user", user);

        return ApiResult.success(map);
    }

    /**
     * /user/update
     *
     * @param user
     * @return
     */
    @PostMapping("/update")
    public ApiResult<User> updateUser(@RequestBody User user) {
        userService.updateById(user);
        return ApiResult.success(user);
    }
}
